package com.zkh360.service.message.exceptions;

import java.util.Arrays;
import java.util.List;

import org.nutz.lang.Lang;

import com.zkh360.service.message.constant.ErrorCode;

/**
 * 
 * 业务异常.
 * 
 * @author 胡浩.
 * @copy 赵传喜
 *
 */
public class BusinessException extends RuntimeException {
	/** 序列化 */
	private static final long serialVersionUID = 1L;
	
	/** 错误代码 */
	private String errorCode;
	/** 错误信息 */
	private List<String> errorMsgs;
	
	/** setter and getter */
	public String getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	public List<String> getErrorMsgs() {
		return errorMsgs;
	}
	
	public String getErrorMsg() {
		return errorMsgs.get(0);
	}

	public void setErrorMsgs(List<String> errorMsgs) {
		this.errorMsgs = errorMsgs;
	}
	
	public void setErrorMsg(String errorMsg) {
		this.errorMsgs = Arrays.asList(errorMsg);
	}

	/**
	 * 私有初始化方法 
	 * 
	 * @param errorCode 错误代码
	 * @param errorMsgs 错误信息
	 */
	private BusinessException(String errorCode, String[] errorMsgs) {
		this.errorCode = errorCode;
		this.errorMsgs = Lang.list(errorMsgs);
	}

	/**
	 * 默认异常.
	 * 
	 * @param errorMsgs 异常信息
	 * @return BusinessException
	 */
	public static BusinessException defaulException(String... errorMsgs) {
		return new BusinessException(ErrorCode.CODE_FAILURE, errorMsgs);
	}

	/**
	 * 自定义异常.
	 * 
	 * @param errorCode 异常代码
	 * @param errorMsg  异常信息
	 * @return BusinessException
	 */
	public static BusinessException customException(String errorCode,
			String errorMsg) {
		return new BusinessException(errorCode, Lang.array(errorMsg));
	}

	/**
	 * 自定义异常.
	 * 
	 * @param errorCode 错误代码
	 * @param errorMsgs 异常信息
	 * @return BusinessException
	 */
	public static BusinessException customException(String errorCode,
			String[] errorMsgs) {
		return new BusinessException(errorCode, errorMsgs);
	}

	/**
	 * 自定义异常.
	 * 
	 * @param errorCode 异常代码
	 * @param errorMsgs 异常信息
	 * @return BusinessException
	 */
	public static BusinessException customException(String errorCode,
			List<String> errorMsgs) {
		return new BusinessException(errorCode,
				Lang.collection2array(errorMsgs));
	}

}
